package com.zheng.upms.rpc.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zheng.common.annotation.BaseService;
import com.zheng.common.base.BaseServiceImpl;
import com.zheng.upms.dao.mapper.UpmsUserPermissionMapper;
import com.zheng.upms.dao.mapper.UpmsUserSystemMapper;
import com.zheng.upms.dao.model.UpmsUserPermission;
import com.zheng.upms.dao.model.UpmsUserSystem;
import com.zheng.upms.rpc.api.UpmsUserPermissionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.Signature;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 用户权限关联表 服务实现类
 * </p>
 *
 * @author likun
 * @since 2017-12-29
 */
@Service
@Transactional
@BaseService
public class UpmsUserPermissionServiceImpl extends BaseServiceImpl<UpmsUserPermissionMapper, UpmsUserPermission> implements UpmsUserPermissionService {
    private static Logger _log = LoggerFactory.getLogger(UpmsUserPermissionServiceImpl.class);
    @Autowired
    private UpmsUserSystemMapper userSystemMapper;

    @Override
    public int permission(JSONArray systems, JSONArray permissions, Integer userId) {
        for (int i = 0; i < permissions.size(); i++) {
            JSONObject permission = permissions.getJSONObject(i);
            Integer permissionId = permission.getInteger("id");
            Integer type = permission.getInteger("type");
            if (permission.getBoolean("checked")) {
                // 新增权限
                UpmsUserPermission upmsUserPermission = new UpmsUserPermission();
                upmsUserPermission.setUserId(userId);
                upmsUserPermission.setPermissionId(permissionId);
                upmsUserPermission.setType(type);
                super.baseMapper.insert(upmsUserPermission);
            } else {
                // 删除权限
                super.baseMapper.delete(new EntityWrapper<UpmsUserPermission>()
                        .eq("permission_id", permissionId)
                        .eq("user_id", userId).eq("type", type));
            }
        }
        for (int i = 0; i < systems.size(); i++) {
            JSONObject system = systems.getJSONObject(i);
            Integer systemId = system.getInteger("id");
            Integer type = system.getInteger("type");
            if (system.getBoolean("checked")) {
                //新增系统权限
                UpmsUserSystem userSystem = new UpmsUserSystem();
                userSystem.setSystemId(systemId);
                userSystem.setUserId(userId);
                userSystem.setType(type);
                userSystemMapper.insert(userSystem);
            } else {
                userSystemMapper.delete(new EntityWrapper<UpmsUserSystem>().eq("system_id", systemId).
                        eq("user_id", userId));
            }
        }
        return systems.size() + permissions.size();
    }
}
